/*
 * =====================================================================================
 *
 *       Filename:  util_ut.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  10/12/2010 07:25:33 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#define BOOST_TEST_MODULE utils_ut 
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include <string>
#include <iostream>
#include "cl_utils.hxx"
#include "cl_string_tokenizer.hxx"
#include "cl_lexical_cast.hxx"
#include "cl_resource_guard.hxx"
#include "cl_idx_table.hxx"
#include "cl_assert.hxx"

using namespace std;
using namespace neo;

BOOST_AUTO_TEST_SUITE(utils_ut)
  struct Test
  {
    ~Test()
    {
      cout << "~Test\n";
    }
  };

BOOST_AUTO_TEST_CASE(stringtokenizer_test)
{
	const string a1("a:b:c");
	const string a2("a:b:c:");
	const string a3(":a:b:c");
	const string a4("");
	const string a5("abcd");
	const string a6(":");
	const string a7("::");

	CLStringTokenizer tokenizer(':');
	CLStringTokenizer::StringVector sv;

	tokenizer.tokenize(a1, sv);
	BOOST_CHECK(sv.empty() == false);
	BOOST_CHECK(sv.size() == 3);
	BOOST_CHECK(sv[0] == "a");
	BOOST_CHECK(sv[1] == "b");
	BOOST_CHECK(sv[2] == "c");
	copy(sv.begin(), sv.end(), ostream_iterator<string>(cout, " "));
	cout << endl;

	sv.clear();
	tokenizer.tokenize(a2, sv);
	BOOST_CHECK(sv.empty() == false);
	BOOST_CHECK(sv.size() == 4);
	BOOST_CHECK(sv[0] == "a");
	BOOST_CHECK(sv[1] == "b");
	BOOST_CHECK(sv[2] == "c");
	BOOST_CHECK(sv[3] == "");
	copy(sv.begin(), sv.end(), ostream_iterator<string>(cout, " "));
	cout << endl;

	sv.clear();
	tokenizer.tokenize(a3, sv);
	BOOST_CHECK(sv.empty() == false);
	BOOST_CHECK(sv.size() == 4);
	BOOST_CHECK(sv[0] == "");
	BOOST_CHECK(sv[1] == "a");
	BOOST_CHECK(sv[2] == "b");
	BOOST_CHECK(sv[3] == "c");
	copy(sv.begin(), sv.end(), ostream_iterator<string>(cout, " "));
	cout << endl;

	sv.clear();
	tokenizer.tokenize(a4, sv);
	BOOST_CHECK(sv.empty() == true);
	BOOST_CHECK(sv.size() == 0);

	sv.clear();
	tokenizer.tokenize(a5, sv);
	BOOST_CHECK(sv.empty() == true);
	BOOST_CHECK(sv.size() == 0);

	sv.clear();
	tokenizer.tokenize(a6, sv);
	BOOST_CHECK(sv.empty() == false);
	BOOST_CHECK(sv.size() == 2);
	BOOST_CHECK(sv[0] == "");
	BOOST_CHECK(sv[1] == "");
	copy(sv.begin(), sv.end(), ostream_iterator<string>(cout, " "));
	cout << endl;
	
	sv.clear();
	tokenizer.tokenize(a7, sv);
	BOOST_CHECK(sv.empty() == false);
	BOOST_CHECK(sv.size() == 3);
	BOOST_CHECK(sv[0] == "");
	BOOST_CHECK(sv[1] == "");
	BOOST_CHECK(sv[2] == "");
	copy(sv.begin(), sv.end(), ostream_iterator<string>(cout, " "));
	cout << endl;
}

BOOST_AUTO_TEST_CASE(lexical_cast)
{
	int i = 100;
	string stri = clLexicalCast<string>(i);
	BOOST_CHECK(stri == string("100"));

	double d = 100.123;
	string strd = clLexicalCast<string>(d);
	BOOST_CHECK(strd == string("100.123"));

	string strii("100");
	int ii = clLexicalCast<int>(strii);
  BOOST_CHECK(ii == 100);

	string strdd("100.123");
	double dd = clLexicalCast<double>(strdd);
  BOOST_CHECK(dd == 100.123);
}

BOOST_AUTO_TEST_CASE(resource_guard)
{
  CLResourceGuard<string, CLNewDeleter<string> > rg(new string("hello"), CLNewDeleter<string>());
  CLResourceGuard<Test, CLNewDeleter<Test> > rg1(new Test, CLNewDeleter<Test>());
  CLResourceGuard<Test, CLNewArrayDeleter<Test> > rg2(new Test[3], CLNewArrayDeleter<Test>());
}

BOOST_AUTO_TEST_CASE(idxtable)
{
	int ai[10] = {15,12,13,14,18,11,10,17,16,19};
	std::vector<int> idxtbl(10);
	clSortIdxTbl(ai, ai + 10, idxtbl.begin());
	for(int i = 0; i < 10; ++i)
	{
		std::cout << "i=" << i
			<< ", idxtbl[i]=" << idxtbl[i]
			<< ",ai[idxtbl[i]]=" << ai[idxtbl[i]]
			<< std::endl;
	}
}

BOOST_AUTO_TEST_CASE(cl_asserter)
{
  ASSERT(0 == 1);
  ASSERT<std::runtime_error>(0 != 1, "throws die die die");
  cout<< "dieing ...\n";
}

BOOST_AUTO_TEST_SUITE_END()

